package com.cq.hd.member.service.impl;

import com.cq.hd.common.constant.Constant;
import com.cq.hd.common.enums.biz.BillTypeEnum;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.utils.LocalDateUtil;
import com.cq.hd.member.api.dto.AdminCommissionBillDto;
import com.cq.hd.member.api.dto.CommissionBillDto;
import com.cq.hd.member.api.vo.AdminCommissionBillPageVo;
import com.cq.hd.member.api.vo.CommissionBillPageVo;
import com.cq.hd.member.api.vo.CommissionBillVo;
import com.cq.hd.member.po.TbCommissionBillPo;
import com.cq.hd.member.mapper.TbCommissionBillMapper;
import com.cq.hd.member.service.TbCommissionBillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 经纪人佣金流水表 服务实现类
 * </p>
 *
 * @author mYunYu
 * @since 2024-01-28
 */
@Service
public class TbCommissionBillServiceImpl extends ServiceImpl<TbCommissionBillMapper, TbCommissionBillPo> implements TbCommissionBillService {

    @Override
    public List<CommissionBillVo> listCommissionBill(CommissionBillDto commissionBillDto) {
        Long userId = commissionBillDto.getUserId();
        if (userId == null) {
            return null;
        }

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startLocalDateTime = now.plusYears(-1);
        commissionBillDto.setStartLocalDateTime(LocalDateUtil.toLocalDateTimeToString(startLocalDateTime, Constant.YYYY_MM_DD_HH_MM_SS));

        // 查询用户最近一年的佣金流水数据
        List<CommissionBillPageVo> commissionBillPageVos = baseMapper.listByDto(commissionBillDto);
        if (CollectionUtils.isEmpty(commissionBillPageVos)) {
            return null;
        }

        Map<String, List<CommissionBillPageVo>> map = new HashMap<>();

        for (CommissionBillPageVo commissionBillPageVo : commissionBillPageVos) {
            LocalDateTime createTime = commissionBillPageVo.getCreateTime();
            String date = LocalDateUtil.toLocalDateToString(createTime.toLocalDate(), "yyyy-MM");

            List<CommissionBillPageVo> commissionBillPageVoList = new ArrayList<>();
            if (map.containsKey(date)) {
                commissionBillPageVoList = map.get(date);
            }

            commissionBillPageVoList.add(commissionBillPageVo);
            map.put(date, commissionBillPageVoList);
        }

        List<CommissionBillVo> commissionBillVos = new ArrayList<>();

        for (String date : map.keySet()) {
            List<CommissionBillPageVo> commissionBillPageVoList = map.get(date);

            BigDecimal monthIncome = BigDecimal.ZERO;
            BigDecimal monthExpend = BigDecimal.ZERO;
            for (CommissionBillPageVo commissionBillPageVo : commissionBillPageVoList) {
                BigDecimal tradeAmt = commissionBillPageVo.getTradeAmt();
                Integer getBillType = commissionBillPageVo.getBillType();
                if (BillTypeEnum.INCOME.getValue().equals(getBillType)) {
                    monthIncome = monthIncome.add(tradeAmt);
                } else if (BillTypeEnum.EXPEND.getValue().equals(getBillType)) {
                    monthExpend = monthExpend.add(tradeAmt);
                }
            }

            String[] splitDate = date.split("-");

            CommissionBillVo commissionBillVo = new CommissionBillVo();
            commissionBillVo.setYear(splitDate[0]);
            commissionBillVo.setMonth(splitDate[1]);
            commissionBillVo.setMonthIncome(monthIncome);
            commissionBillVo.setMonthExpend(monthExpend);
            commissionBillVo.setCommissionBills(commissionBillPageVoList);
            commissionBillVos.add(commissionBillVo);
        }

        return commissionBillVos;
    }

    @Override
    public Page<AdminCommissionBillPageVo> pageCommissionBill(AdminCommissionBillDto adminCommissionBillDto) {
        PageHelper.startPage(adminCommissionBillDto.getPageNum(), adminCommissionBillDto.getPageSize());
        List<AdminCommissionBillPageVo> adminCommissionBillPageVos = baseMapper.listCommissionBillByDto(adminCommissionBillDto);
        PageInfo<AdminCommissionBillPageVo> pageInfo = new PageInfo<>(adminCommissionBillPageVos);
        return Page.newPageWithAllFields(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getPages());
    }

}
